/**
 * Created by weikexin .
 * description:
 **/
// app.js
const ald = require('./util/sdk/ald-stat.js')
import distribution from './util/distribution'
let model
const app = getApp()
App({ 
  globalData: {
    userInfo: null,
    test: model,
    isIPhoneX: false
  },
  onShow(options) {
    const otherShareTime = wx.getStorageSync('otherShareTime')
    if (otherShareTime) {
      let timestamp = +new Date()
      if (timestamp - otherShareTime > 1296000000) {
        // 1296000000 = 15 days
        wx.removeStorageSync('otherId')
        wx.removeStorageSync('otherShareTime')
      }
    }
    distribution(options)
  },
  onLaunch() {
    // 判断是否为初次进入小程序
    const userId = wx.getStorageSync('userInfo').user_id
    if (userId) {
      // 已登录，不是新用户了
      wx.setStorageSync('isFirstJoin', true)
    }

    let that = this
    /*
     * 获取系统型号进行适配
     * */
    wx.getSystemInfo({
      success(res) {
        model = res.model
        that.globalData.isIPhoneX = res.model.indexOf('iPhone X') > -1
      }
    })
    /*
     * 在进入小程序时清理地址缓存，重新获取
     * */
    wx.removeStorageSync('address')
    /*
     * 更新小程序版本
     * */
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
    })
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
  }
})
